<template>
  <div class="auto form-horizontal">
    <modal :show.sync="show"  v-ref:modal large backdrop="false">
      <header slot="modal-header" class="modal-header">
        <h3 style="color:black" class="modal-title">机表发卡补打发票</h3>
      </header>
      <article slot="modal-body" style = "display: flex;justify-content: center;" id="testbill">
        <div v-if="bill.data">
          {{{ bill.data}}}
        </div>
        <div v-if="replacebill.data">
          {{{ replacebill.data}}}
        </div>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <div class="col-xs-5" v-if="messShow">
          <span style="color: red"><strong>未获取到收据代码，发票不能正常打印，请确认后补打发票</strong></span>
        </div>
        <div class="col-sm-1" v-if="!messShow">
          <span><strong>发票号:{{model.f_bill_id}}</strong></span>
        </div>
        <report-print id='testbill' v-if="billshow" top='55mm' left='60mm' width='100%' height='80%' style='td,th{font-size: 10pt!important;border: 0;font-size: 19px;} tdright {text-align:right}'
         :notrepeat="true" v-if= '(bill.data || replacebill.data) && clean.f_bill_id'></report-print>
        <button class="btn btn-default" @click="clean()">取消</button>
      </footer>
    </modal>
  </div>
</template>
<script>
/**
*票据补打
*/
import { DataModel } from 'vue-client'
export default {
  title: '机表发卡票据',
  data () {
    return {
      bill: new DataModel('rs/report/machineCard_bill2', {reprint: "'正常'"}),
      replacebill: new DataModel('rs/report/replace_sell', {reprint: "'正常'"}),
      model: {
        f_bill_id: '',
        f_billcoll_id: ''
      },
      billshow: false,
      messShow: false
    }
  },
  props: ['show', 'data'],
  watch: {
    'show' (val) {
      this.billshow = true
      if (val === true) {
        this.getBillNum()
      }
    }
  },
  methods: {
    clean () {
      this.$dispatch('toggle')
    },
    getBillNum () {
      let tablename = 't_billcollec'
      let condition = ' f_bill_state = ' + "'" + '启用' + "'" + ' and f_bill_type = ' + "'" + '国税发票' + "'"
      this.$SqlService.singleTable(tablename, condition).then((res) => {
        console.log('获取票号', res.data[0])
        if (res.data[0]. f_bill_id > res.data[0].f_end_number) {
          res.data[0]. f_bill_state = '停用'
          this.$BillService.stopBill(res.data[0])
          this.messShow = true
        } else {
          this.model.f_bill_id = res.data[0].f_bill_id
          this.model.f_billcoll_id = res.data[0].id
        }
      }).catch((error) => {
        console.log('获取发票号出错!!!', error)
        this.messShow = true
      })
    }
  },
  events: {
    'print-success' () {
      this.$PrintService.modifyBillNumber(this.model, this.data).then((res) => {
        this.$dispatch('success', '发票打印', this.data, res)
      }).catch((error) => {
        this.$dispatch('error', '发票打印', this.data, error)
      })
    },
    'print-error' () {
      this.$dispatch('error', '发票打印', this.data, null)
    }
  }
}
</script>
